using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLThuChi.Repositories
{
	#region using
	using QLThuChi.Models;
	#endregion
	public class PhieuThuRepository
	{
		public static List<PhieuThu> SelectAll()
		{
			return DataContext.Instance.PhieuThus.ToList();
		}

		public static PhieuThu SelectByID(int maphieu)
		{
			return DataContext.Instance.PhieuThus.FirstOrDefault(item => item.MaPhieu == maphieu );
		}

		public static bool Insert(PhieuThu obj)
		{
			try
			{
				DataContext.Instance.PhieuThus.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Delete(int maphieu)
		{
			try
			{
				var delitem = DataContext.Instance.PhieuThus.FirstOrDefault(item => item.MaPhieu == maphieu );
				DataContext.Instance.PhieuThus.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static List<PhieuThu> RetrieveByID(int maphieu)
		{
			return (from item in DataContext.Instance.PhieuThus where  item.MaPhieu == maphieu  select item).ToList();
		}

		public static List<PhieuThu> SelectByMaCongTrinhGiaiDoan(int macongtrinhgiaidoan)
		{
			var lstItem = (from item in DataContext.Instance.PhieuThus where item.MaCongTrinhGiaiDoan == macongtrinhgiaidoan select item).ToList();
			return lstItem;
		}

        public static List<PhieuThu> SelectByMaCongTrinh(int macongtrinh)
        {
            var lstItem = (from item in DataContext.Instance.PhieuThus where item.CongTrinh_GiaiDoan.MaCongTrinh == macongtrinh select item).ToList();
            return lstItem;
        }

		public static List<PhieuThu> SelectByMaNguoiLapPhieu(int manguoilapphieu)
		{
			var lstItem = (from item in DataContext.Instance.PhieuThus where item.MaNguoiLapPhieu == manguoilapphieu select item).ToList();
			return lstItem;
		}

        public static List<PhieuThu> SelectByCongTrinhGiaiDoan(int macongtrinh, int magiaidoan)
        {
            var lstItem = (from item in DataContext.Instance.PhieuThus where item.CongTrinh_GiaiDoan.MaCongTrinh == macongtrinh && item.CongTrinh_GiaiDoan.MaGiaiDoan == magiaidoan select item).ToList();
            return lstItem;
        }
	}
}